//
//  NewTaskFooter.m
//  RRQS
//
//  Created by kyjun on 16/3/24.
//
//

#import "NewTaskFooter.h"

@interface NewTaskFooter ()<AMapSearchDelegate>
@property(nonatomic,strong) UIView* topView;
@property(nonatomic,strong) UIButton* btnIcon;
@property(nonatomic,strong) UILabel* labelSendAddress;
@property(nonatomic,strong) UILabel* labelReGeocode;
@property(nonatomic,strong) UIView* bottomView;
@property(nonatomic,strong) UIButton* btnRush;
@property (nonatomic, strong) AMapSearchAPI *search;
@property(nonatomic,strong) UILabel* labelDistance;
@end

@implementation NewTaskFooter


-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        self.backgroundColor = [UIColor colorWithRed:255/255.f green:255/255.f blue:255/255.f alpha:1.0];
        [self layoutUI];
        [self layoutConstraints];
    }
    return self;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    CALayer* border = [[CALayer alloc]init];
    border.frame = CGRectMake(0, CGRectGetHeight(self.topView.frame)-1, CGRectGetWidth(self.topView.frame), 1.f);
    border.backgroundColor = [UIColor colorWithRed:231/255.0f green:231/255.0f blue:231/255.0f alpha:1.0f].CGColor;
    [self.topView.layer addSublayer:border];
    self.labelDistance.frame = CGRectMake(CGRectGetMaxX(self.labelSendAddress.frame),self.topView.center.y, 93 / WIDTH_6S_SCALE, 25 / HEIGHT_6S_SCALE);
}

#pragma mark =====================================================  user inferface layout
-(void)layoutUI{
    [self addSubview:self.topView];
    [self.topView addSubview:self.btnIcon];
    [self.topView addSubview:self.labelSendAddress];
    [self.topView addSubview:self.labelReGeocode];
    [self addSubview:self.bottomView];
    [self.bottomView addSubview:self.btnRush];
    
    [self.topView addSubview:self.labelDistance];
    
}

-(void)layoutConstraints{
    NSArray* formats = @[ @"H:|-0-[topView]-0-|", @"V:|-0-[topView(>=60)]",
                          @"H:|-0-[bottomView]-0-|", @"V:[topView][bottomView(==60)]-0-|",
                          @"H:|-10-[btnIcon(==25)]", @"V:|-17-[btnIcon(==25)]",
                          @"H:[btnIcon]-10-[labelSendAddress]-105-|",
                          @"H:|-45-[labelReGeocode]-105-|", @"V:|-0-[labelSendAddress(==60)][labelReGeocode(>=0)]-0-|",
                          @"H:|-10-[btnRush]-10-|", @"V:|-10-[btnRush]-10-|"
                          
                          ];
    NSDictionary* metrics = nil;
    NSDictionary* views = @{ @"topView":self.topView, @"bottomView":self.bottomView, @"btnIcon":self.btnIcon, @"labelSendAddress":self.labelSendAddress, @"labelReGeocode":self.labelReGeocode, @"btnRush":self.btnRush};
    
    for (NSString* format in formats) {
        NSArray* constraints = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:metrics views:views];
        [self addConstraints:constraints];
    }
    
}

/* 逆地理编码回调. */
- (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response
{
    if (response.regeocode != nil)
    {
        self.labelReGeocode.text = response.regeocode.formattedAddress;
    }
}

#pragma mark =====================================================  SEL
-(IBAction)grabOneTouch:(UIButton*)sender{
//    if(self.delegate && [self.delegate respondsToSelector:@selector(NewTaskGrabOne:)]){
//        [self.delegate NewTaskGrabOne:self.entity];
//    }
    if (self.isWaimOrPaot) {
        if(self.delegate && [self.delegate respondsToSelector:@selector(NewTaskPaoTui:)]){
            [self.delegate NewTaskPaoTui:self.waiMaiDodelHList];
        }
    }else{
        if(self.delegate && [self.delegate respondsToSelector:@selector(NewTaskWaiMai:)]){
            [self.delegate NewTaskWaiMai:self.waiMaiDodelList];
        }
    }
    
}

#pragma mark =====================================================  property package
- (void)setWaiMaiDodelHList:(WJWaiMaiDodelHList *)waiMaiDodelHList // 跑腿
{
    if(waiMaiDodelHList){
        self.isWaimOrPaot = YES;
        _waiMaiDodelHList = waiMaiDodelHList;
        self.labelSendAddress.text = waiMaiDodelHList.send_address;
        self.labelReGeocode.text =  waiMaiDodelHList.map_location;
        
        NSString *strMoney;
        strMoney = [NSString stringWithFormat:@"%@km送货",waiMaiDodelHList.location_get];
        NSMutableAttributedString *attributeStr  = [[NSMutableAttributedString alloc]initWithString:strMoney];
        [attributeStr addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12.f],NSForegroundColorAttributeName:[UIColor colorWithRed:248/255.f green:159/255.f blue:39/255.f alpha:1.0]} range:[strMoney rangeOfString:[NSString stringWithFormat:@"%@km",waiMaiDodelHList.location]]];
        _labelDistance.attributedText = attributeStr;
        if ([waiMaiDodelHList.type isEqualToString:@"buy"]) {
            [_btnRush setTitle:@"帮你买" forState:UIControlStateNormal];
        }
        if ([waiMaiDodelHList.type isEqualToString:@"send"]) {
            [_btnRush setTitle:@"帮你送" forState:UIControlStateNormal];
        }
        if ([waiMaiDodelHList.type isEqualToString:@"get"]) {
            [_btnRush setTitle:@"帮你取" forState:UIControlStateNormal];
        }
        
    }
}
- (void)setWaiMaiDodelList:(WJWaiMaiDodelList *)waiMaiDodelList // 外卖
{
    if(waiMaiDodelList){
        self.isWaimOrPaot = NO;
        _waiMaiDodelList = waiMaiDodelList;
        self.labelSendAddress.text = waiMaiDodelList.sendaddress;
        self.labelReGeocode.text =  waiMaiDodelList.address_road;
        [_btnRush setTitle:@"抢单" forState:UIControlStateNormal];
    }
}

-(void)setEntity:(MOrder *)entity{
    if(entity){
        _entity = entity;
        MSubOrder* item = (MSubOrder*)[entity.arraySubOrder firstObject];
        self.labelSendAddress.text = entity.address;
        if([WMHelper isEmptyOrNULLOrnil:entity.addressRoad]){
            if([WMHelper isEmptyOrNULLOrnil:item.phoneAddress]){
                self.labelReGeocode.text =  @"";
            }else{
                NSArray* empty = [item.phoneAddress componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@","]];
                if(empty.count == 2){
                    AMapReGeocodeSearchRequest *regeo = [[AMapReGeocodeSearchRequest alloc] init];
                    regeo.location = [AMapGeoPoint locationWithLatitude:[[empty lastObject] doubleValue] longitude:[[empty firstObject] doubleValue]];
                    regeo.requireExtension = YES;
                    regeo.radius = 1000;
                    [self.search AMapReGoecodeSearch:regeo];
                }else{
                    self.labelReGeocode.text =  @"";
                }
            }
        }else{
            self.labelReGeocode.text = entity.addressRoad;
        }
    }
}

-(UIView *)topView{
    if(!_topView){
        _topView  = [[UIView alloc]init];
        _topView.translatesAutoresizingMaskIntoConstraints = NO;
    }
    return _topView;
}

-(UIButton *)btnIcon{
    if(!_btnIcon){
        _btnIcon = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btnIcon setImage:[UIImage imageNamed:@"icon-sendStatus-light"] forState:UIControlStateNormal];
        _btnIcon.userInteractionEnabled = NO;
        _btnIcon.translatesAutoresizingMaskIntoConstraints = NO;
    }
    return _btnIcon;
}

-(UILabel *)labelSendAddress{
    if(!_labelSendAddress){
        _labelSendAddress = [[UILabel alloc]init];
        _labelSendAddress.font = [UIFont systemFontOfSize:14.f];
        _labelSendAddress.lineBreakMode = NSLineBreakByCharWrapping;
        _labelSendAddress.numberOfLines = 0;
        _labelSendAddress.translatesAutoresizingMaskIntoConstraints = NO;
    }
    return _labelSendAddress;
}

-(UILabel *)labelReGeocode{
    if(!_labelReGeocode){
        _labelReGeocode = [[UILabel alloc]init];
        _labelReGeocode.textColor = [UIColor grayColor];
        _labelReGeocode.font = [UIFont systemFontOfSize:12.f];
        _labelReGeocode.lineBreakMode = NSLineBreakByCharWrapping;
        _labelReGeocode.numberOfLines = 0;
        _labelReGeocode.translatesAutoresizingMaskIntoConstraints = NO;
    }
    return _labelReGeocode;
}

-(UIView *)bottomView{
    if(!_bottomView){
        _bottomView = [[UIView alloc]init];
        _bottomView.translatesAutoresizingMaskIntoConstraints = NO;
    }
    return _bottomView;
}

-(UIButton *)btnRush{
    if(!_btnRush){
        _btnRush = [UIButton buttonWithType:UIButtonTypeCustom];
//        _btnRush.backgroundColor = [UIColor colorWithRed:107/255.f green:187/255.f blue:217/255.f alpha:1.0];
        _btnRush.backgroundColor = getColor(@"27B088");
        [_btnRush setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//        [_btnRush setTitle:@"帮你买" forState:UIControlStateNormal];
        _btnRush.layer.masksToBounds = YES;
        _btnRush.layer.cornerRadius = 5.f;
        [_btnRush addTarget:self action:@selector(grabOneTouch:) forControlEvents:UIControlEventTouchUpInside];
        _btnRush.translatesAutoresizingMaskIntoConstraints = NO;
    }
    return _btnRush;
}

-(AMapSearchAPI *)search{
    if(!_search){
        _search = [[AMapSearchAPI alloc] init];
        _search.delegate = self;
    }
    return _search;
}
-(UILabel *)labelDistance{
    if(!_labelDistance){
        _labelDistance = [[UILabel alloc]init];
        _labelDistance.font = [UIFont systemFontOfSize:12.f];
        _labelDistance.textColor =  [UIColor colorWithRed:113/255.f green:113/255.f blue:113/255.f alpha:1.0];
//        _labelDistance.backgroundColor = getColor(blueColor);
    }
    return _labelDistance;
}
@end
